package com.qys.livesMall.event.listener;

import com.qys.livesMall.anchor.event.GiveGiftEvent;
import com.qys.livesMall.anchor.event.pojo.GiveGiftEventPOJO;
import com.qys.livesMall.anchor.model.pojo.LivesGiftRecordPOJO;
import com.qys.livesMall.anchor.model.vo.lives.app.GiftListVO;
import com.qys.livesMall.anchor.service.AnchorLivesGiftRecordService;
import com.qys.livesMall.cache.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;

import static com.qys.livesMall.anchor.constants.RedisConstant.LIVE_ROOM_GIFT_LIST;

@Slf4j
@Component
public class GiveGiftListener implements ApplicationListener<GiveGiftEvent> {

    @Autowired
    private AnchorLivesGiftRecordService livesGiftRecordService;
    @Autowired
    private RedisCache redisCache;

    @Override
    @Async
    public void onApplicationEvent(GiveGiftEvent event) {
        GiveGiftEventPOJO pojo = (GiveGiftEventPOJO) event.getSource();
        //增加赠送记录
        livesGiftRecordService.createRecord(LivesGiftRecordPOJO
                .builder()
                .livesGift(pojo.getLivesGift())
                .toAnchorId(pojo.getToAnchorId())
                .livesSessionId(pojo.getLivesSessionId())
                .livesRoomId(pojo.getLivesRoomId())
                .quantity(pojo.getQuantity())
                .userId(pojo.getUserId())
                .build());
        log.info("监听到用户【{}】,赠送【{}】礼物【{}】给主播【{}】,", pojo.getUserId(),pojo.getQuantity(),pojo.getLivesGift().getGiftName(),pojo.getToAnchorId());

        //获取缓存中礼物数据
        Map<String, GiftListVO> cacheMap = redisCache.getCacheMap(getGiftListKey(pojo.getLivesSessionId()));
        if(!CollectionUtils.isEmpty(cacheMap)){
            GiftListVO giftByMe= cacheMap.get(pojo.getUserId().toString());
            if (giftByMe == null) {
                //未赠送过礼物
                GiftListVO gift = new GiftListVO();
                gift.setUserId(pojo.getUserId());
                gift.setGiftAmount(BigDecimal.valueOf(pojo.getQuantity()));
                cacheMap.put(String.valueOf(pojo.getUserId()), gift);
            } else {
                //赠送过礼物
                giftByMe.setGiftAmount(giftByMe.getGiftAmount().add(pojo.getLivesGift().getGiftGold().multiply(BigDecimal.valueOf(pojo.getQuantity()))).setScale(4, RoundingMode.FLOOR));
                cacheMap.put(String.valueOf(pojo.getUserId()), giftByMe);
            }
        } else {
            GiftListVO gift = new GiftListVO();
            gift.setUserId(pojo.getUserId());
            gift.setGiftAmount(BigDecimal.valueOf(pojo.getQuantity()));
            cacheMap.put(String.valueOf(pojo.getUserId()), gift);
        }
        redisCache.setCacheMap(getGiftListKey(pojo.getLivesSessionId()),cacheMap);
    }

    private String getGiftListKey(Long livesSessionId) {
        return LIVE_ROOM_GIFT_LIST + livesSessionId;
    }

}
